package com.zb.manager.controller;

import com.zb.model.TCategory;
import com.zb.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**  
* <p>Title: CategoryController</p>  
* <p>Description: �����ֵķ������ʵ�ַ������ɾ�Ĳ�</p>  
*/
@Controller
@RequestMapping("/categroy")
public class CategoryControllers {

	@Autowired
	private CategoryService cs;
	
	/**
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/cate_manager")
	public String cate_manager() throws Exception{
		return "categroy/cate_manager";
	}
	
	/**
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/brand_manager")
	public String brand_manager() throws Exception{
		return "categroy/brand_manager";
	}
	
	/**
	 * 
	 * NOT USE
	 */
	@GetMapping("/addcate")
	public String cate_add() throws Exception{
		return "admin/ok";
	}
	
	/**
	 * SELECT CATE
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectCate")
	public String selectCate(HttpSession session)throws Exception{
		List<TCategory> cateList=cs.selectCate();
		session.setAttribute("cateList", cateList);
		return "admin/catelist";
	}
	
	/**
	 *  ADD CATE
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addCate")
	public String addCate(HttpServletRequest request,HttpServletResponse response)throws Exception{
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		TCategory cate=new TCategory();
		cate.setCategoryName(request.getParameter("catename"));
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
//        String createtime = df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ��
        Date date=new Date();
        cate.setCreateTime(date);
		String uuid=UUID.randomUUID().toString();
		cate.setId(uuid);
		cate.setImgPath("static/images/"+request.getParameter("imgPath"));
		cs.addCate(cate);
		JOptionPane.showMessageDialog(null, "ADD OK");
		return "admin/ok";
	}
	
	String uploadUrl="http://localhost:8080/moyissm/file/ajax/upload_file";
	
	/**
	 *  ADD CATE
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/uploadCate")
	public Object uploadCate(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String result="";
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> fileList = multipartRequest.getFiles("file");
        if(fileList == null || fileList.size() == 0){
        	return null;
            //return addResultMapMsg(false,"请上传文件,注意文件的name属性为file");
        }
        MultipartFile file = fileList.get(0);
        try {

            // 换行符
            final String newLine = "\r\n";
            final String boundaryPrefix = "--";
            // 定义数据分隔线
            String BOUNDARY = "========7d4a6d158c9";
            // 服务器的域名
            URL url = new URL(uploadUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置为POST情
            conn.setRequestMethod("POST");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求头参数
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

            OutputStream out = new DataOutputStream(conn.getOutputStream());

            // 上传文件

            StringBuilder sb = new StringBuilder();
            sb.append(boundaryPrefix);
            sb.append(BOUNDARY);
            sb.append(newLine);
            // 文件参数,photo参数名可以随意修改
            sb.append("Content-Disposition: form-data;name=\"photo\";filename=\"" + file.getOriginalFilename()
                    + "\"" + newLine);
            sb.append("Content-Type:application/octet-stream");
            // 参数头设置完以后需要两个换行，然后才是参数内容
            sb.append(newLine);
            sb.append(newLine);

            // 将参数头的数据写入到输出流中
            out.write(sb.toString().getBytes());

            // 数据输入流,用于读取文件数据
            DataInputStream in = new DataInputStream(file.getInputStream());
            byte[] bufferOut = new byte[1024*8];
            int bytes = 0;
            // 每次读8KB数据,并且将文件数据写入到输出流中
            while ((bytes = in.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
            }
            // 最后添加换行
            out.write(newLine.getBytes());
            in.close();

            // 定义最后数据分隔线，即--加上BOUNDARY再加上--。
            byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine)
                    .getBytes();
            // 写上结尾标识
            out.write(end_data);
            out.flush();
            out.close();

            // 定义BufferedReader输入流来读取URL的响应

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                result += line; //这里读取的是上边url对应的上传文件接口的返回值，读取出来后，然后接着返回到前端，实现接口中调用接口的方式
            }
        } catch (Exception e) {
            System.out.println("发送POST请求出现异常！" + e);
            e.printStackTrace();
        }
        return result;

	}
	
	/**
	 * MODIFIY
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/reCate")
	public String reCate(HttpServletRequest request)throws Exception{
		request.setCharacterEncoding("UTF-8"); // 设置服务器以UTF-8的编码接收数据
		TCategory cate=new TCategory();
		cate.setId(request.getParameter("cateid"));
		cate.setCategoryName(request.getParameter("catename"));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//�������ڸ�ʽ
        String updateTime = df.format(new Date());// new Date()Ϊ��ȡ��ǰϵͳʱ��
        Date date=new Date();
        cate.setUpdateTime(date);
		cate.setImgPath("");
		cs.reCate(cate);
		JOptionPane.showMessageDialog(null, "MODIFIY OK");
		return "admin/ok";
	}
	
	/**
	 * DELETE
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteCate")
	public String deleteCate(HttpServletRequest request)throws Exception{
		TCategory cate=new TCategory();
		String cateid = request.getParameter("cateid");
		cs.deleteCate(cateid);
		return "admin/catelist";
	}
	
}
